# Remove branches from after 2018
ar.cox.df <- ar.cox.df[-which(ar.cox.df$c.year>=2018),]
max(ar.cox.df$c.year)
min(ar.cox.df$c.year)
ar.cox.df1 <- do.call("rbind",ar.cox[1:10000])
max(ar.cox.df1$c.year)
ar.cox.df1 <- do.call("rbind",ar.cox[1:10000])
ar.cox.df2 <- do.call("rbind",ar.cox[10001:20000])
ar.cox.df3 <- do.call("rbind",ar.cox[20001:30000])
ar.cox.df4 <- do.call("rbind",ar.cox[30001:40000])
ar.cox.df5 <- do.call("rbind",ar.cox[40001:50000])
ar.cox.df6 <- do.call("rbind",ar.cox[50001:60000])
ar.cox.df7 <- do.call("rbind",ar.cox[60001:70000])
ar.cox.df8 <- do.call("rbind",ar.cox[70001:80000])
ar.cox.df9 <- do.call("rbind",ar.cox[80001:length(ar.cox)])
ar.cox.df <- bind_rows(ar.cox.df9,bind_rows(ar.cox.df8,bind_rows(ar.cox.df7,bind_rows(ar.cox.df6,bind_rows(ar.cox.df5,bind_rows(ar.cox.df4,bind_rows(ar.cox.df3,bind_rows(ar.cox.df1,ar.cox.df2))))))))
rm(list=paste0("ar.cox.df",1:9))
ar.cox.df <- as_tibble(ar.cox.df)
ar.cox.df <- ar.cox.df %>% arrange(id)
# Remove observations that had branches with bills from multiple countries
ar.cox.df <- ar.cox.df[-which(ar.cox.df$id %in% ar.cox.df$id[which(ar.cox.df$country=="-88")]),]
# Make sure numeric columns are numeric
ar.cox.df[,c(1:6,9:10)] <- apply(ar.cox.df[,c(1:6,9:10)], 2, function(x){as.numeric(x)})
# Remove observations with any branches from before 1970
ar.cox.df <- ar.cox.df[-which(ar.cox.df$id %in% ar.cox.df$id[which(ar.cox.df$o.year < 1970)]),]
ar.cox.df <- ar.cox.df[-which(ar.cox.df$id %in% unique(ar.cox.df$id[which(ar.cox.df$c.year<1970)])),]
# Remove branches from after 2018
ar.cox.df <- ar.cox.df[-which(ar.cox.df$c.year>2018),]
dim(ar.cox.df)
max(ar.cox.df$c.year)
countrycode(ar.cox.df$country,origin = "country.name",destination = "country.name")
ar.cox.df$country
# Keep only national-level policies (not sub-national or multinational)
national <- dat[which(dat$national==1 & dat$multinational==0),]
# Extract FAOLEX IDs for amends, repeals, amendedby, and repealedby
national$f_amends <- apply(str_extract_all(national$amends,pattern = "faoc[0-9]{6}",simplify = T),1,function(x)paste0(x[-which(x=="")],collapse = "; "))
national$f_amends[which(national$f_amends=="NA")] <- NA
national$f_amended_by <- apply(str_extract_all(national$amended_by,pattern = "faoc[0-9]{6}",simplify = T),1,function(x)paste0(x[-which(x=="")],collapse = "; "))
national$f_amended_by[which(national$f_amended_by=="NA")] <- NA
national$f_repeals <- apply(str_extract_all(national$repeals,pattern = "faoc[0-9]{6}",simplify = T),1,function(x)paste0(x[-which(x=="")],collapse = "; "))
national$f_repeals[which(national$f_repeals=="NA")] <- NA
national$f_repealed_by <- apply(str_extract_all(national$repealed_by,pattern = "faoc[0-9]{6}",simplify = T),1,function(x)paste0(x[-which(x=="")],collapse = "; "))
national$f_repealed_by[which(national$f_repealed_by=="NA")] <- NA
### 1.2 Make dataframe with only urls that amend no other policy as the base and add all amendments to the base ####
cascades <- tibble(base=national$faolex_id_no[which(is.na(national$f_amends) & is.na(national$f_repeals))])
amendments <- str_split(national$f_amended_by[which(is.na(national$f_amends) & is.na(national$f_repeals))],pattern = "; ",simplify = T)
colnames(amendments) <- c(paste0("a",1:(ncol(amendments))))
amendments <- as_tibble(amendments)
cascades <- bind_cols(cascades,amendments)
cascades <- as_tibble(apply(cascades,2,function(x){
x <- unlist(x)
if(length(which(x==""))>0){
x[which(x=="")] <- NA
}
# Remove references to laws outside of the dataset
x[which(x %in% national$faolex_id_no == F)] <- NA
x
}))
extra <- matrix(NA, ncol = 300-ncol(cascades), nrow = nrow(cascades))
colnames(extra) <- paste0("a",(ncol(cascades)+1):300)
cascades <- bind_cols(cascades, as_tibble(extra))
cl <- makeCluster(8)
nat <- national %>% select(f_amended_by, url, f_repealed_by)
clusterExport(cl=cl,c("nat","cascades"))
clusterEvalQ(cl=cl, library(stringr))
deg2 <- pblapply(cl=cl,which(is.na(cascades[,2])==F),function(i){
idx <- unlist(cascades[i,][-c(1,which(is.na(cascades[i,])))])
if(length(idx)>0){
temp <- c(unlist(str_split(nat$f_amended_by[which(nat$faolex_id_no %in% idx)],pattern = "; ")),
unlist(str_split(nat$f_repealed_by[which(nat$faolex_id_no %in% idx)],pattern = "; ")))
temp <- temp[-which(is.na(temp))]
temp <- unlist(c(cascades[i,1],idx, temp))
if(is.null(temp)){
temp <- NA
} else {
if(sum(duplicated(temp))>0){
temp <- temp[-which(duplicated(temp)==1)]
}
}
temp <- t(data.frame(temp))
temp <- c(temp,rep(NA,300-ncol(temp)))
temp
} else {
rep(NA,300)
}
})
deg2_df <- plyr::ldply(deg2)
cascades[which(is.na(cascades[,2])==F),] <- deg2_df
# Remove cascades without dates
cascades <- pbapply(cascades,2,function(x){
idx <- national$faolex_id_no[which(is.na(national$date) & national$faolex_id_no %in% unlist(x))]
x[which(unlist(x) %in% idx)] <- NA
x
})
if(length(which(is.na(cascades[,1])))>0){
cascades <- cascades[-which(is.na(cascades[,1])),]
}
# Add repeals of original policy (if multiple repeals, take only earliest repeal)
cascades <- bind_cols(as_tibble(cascades), as_tibble(data.frame(repeal=rep(NA,nrow(cascades)))))
lookup <- which(cascades$base %in% national$faolex_id_no[which(is.na(national$f_repealed_by)==F &
national$faolex_id_no %in% cascades$base)])
repeal <- pblapply(lookup,function(i){
temp <- national$f_repealed_by[which(national$faolex_id_no==cascades$base[i])]
o.date <- national$date[which(national$faolex_id_no==cascades$base[i])]
if(is.na(temp)==F){
temp <- unlist(str_split(temp,pattern = "; "))
dates <- rep(NA, length(temp))
for(j in 1:length(temp)){
idx <- which(national$faolex_id_no==temp[j])
if(length(idx)>0){
if(is.na(national$date[idx])==F & national$date[idx] >= o.date){
dates[j] <- national$date[idx]
}
}
}
if(sum(is.na(dates)==F)>0){
temp[which(dates==min(dates,na.rm = T))[1]]
}
} else {
"ERR"
}
})
repeal[which(lengths(repeal)=0)] <- NA
repeal_df <- plyr::ldply(repeal)
cascades$repeal[lookup] <- unlist(repeal_df)
## Clean up
# Remove references to non-national policies
cascades <- as_tibble(apply(cascades,2,function(x){
x <- unlist(x)
idx <- which(x %in% national$faolex_id_no == F)
if(length(idx)>0){
x[idx] <- NA
}
x
}))
# Remove duplicates
toremove <- pbsapply(1:nrow(cascades),function(x){
which(duplicated(unlist(cascades[x,]),incomparables = c(NA,"")))
})
for(j in 1:nrow(cascades)){
cascades[j,toremove[[j]]] <- NA
}
# Remove levels from character variables
cascades <- apply(cascades,2,function(x){
levels(x) <- NULL
x
})
# Left-justify all entries (except repeal)
cascades[,-which(colnames(cascades)=="repeal")] <- t(pbapply(cascades[,-which(colnames(cascades)=="repeal")],1,function(x){
if(sum(is.na(x))>0){
temp <- x[-which(is.na(x))]
x[which(is.na(x)==F)] <- NA
x[1:length(temp)] <- temp
}
x
}))
# Remove empty columns
colstoremove <- apply(cascades,2,function(x){
which(length(which(is.na(x)==F))==0)
})
cascades <- cascades[,which(lengths(colstoremove)!=1)]
colnames(cascades) <- c("base",paste0("a",1:(ncol(cascades)-2)),"repeal")
cascades <- as_tibble(cascades)
### 1.3 Make policy cascades into survival-format dataframe ####
nat <- national[,which(colnames(national) %in% c("faolex_id_no","date","country","subject","keyword", "document_type", "repealed_by"))]
cl <- makeCluster(8)
clusterExport(cl, c("cascades","nat"))
clusterEvalQ(cl, library("stringr"))
system.time(
ar.cox <- pblapply(cl=cl,1:nrow(cascades), function(i){
if(length(which(is.na(cascades[i,])))>0){
id.idx <- cascades[i,-c(which(is.na(cascades[i,])))]
} else {
id.idx <- cascades[i,]
}
idx <- which(nat$faolex_id_no %in% unlist(id.idx))
date <- nat$date[idx]
if(sum(is.na(date))==0){
temp <- as.data.frame(matrix(NA, nrow = (length(min(date):2019) + sum(duplicated(date))), ncol = 13))
colnames(temp) <- c("id","time","tstop","ar","ar.num","repealed","o.subject","o.keywords","o.year","c.year","country","o.doctype","faolex_id_no")
temp$id <- i
if(sum(duplicated(date))==0){
temp$c.year <- min(date):2019
} else {
temp$c.year <- sort(c(min(date):2019, date[which(duplicated(date))]))
}
temp$ar <- 0
temp$ar[temp$c.year %in% date[-which(date==min(date))[1]]] <- 1
if(length(date)==2 & sum(duplicated(date))==1){
temp <- temp[-which(temp$ar==1)[1],]
}
temp$ar.num <- 0
temp$time <- 0:(nrow(temp)-1)
if(sum(temp$ar)>0){
for(j in 1:sum(temp$ar)){
if(which(temp$ar==1)[j]!=nrow(temp)){
temp$time[(which(temp$ar==1)[j]+1):nrow(temp)] <- 0:(length((which(temp$ar==1)[j]+1):nrow(temp))-1)
temp$ar.num[(which(temp$ar==1)[j]+1):nrow(temp)] <- j
}
}
}
temp$tstop <- temp$time+1
temp$repealed <- 0
if("repeal" %in% names(cascades[i,-which(is.na(cascades[i,]))])){
temp$repealed[which(temp$c.year == nat$date[which(nat$faolex_id_no==cascades$repeal[i])]):nrow(temp)] <- 1
if(sum(temp$repealed)>1){
temp <- temp[-((which(temp$repealed==1)[2]):nrow(temp)),]
}
}
o.idx <- which(nat$faolex_id_no==unlist(cascades[i,1]))
temp$o.subject <- nat$subject[o.idx]
temp$o.keywords <- nat$keyword[o.idx]
temp$o.year <- nat$date[o.idx]
temp$country <- ifelse(length(unique(nat$country[idx]))==1,nat$country[o.idx],-88)
temp$o.doctype <- nat$document_type[o.idx]
for(j in unlist(id.idx)){
temp$faolex_id_no[which(temp$c.year==nat$date[which(nat$faolex_id_no==j)] & is.na(temp$faolex_id_no))[1]] <- j
}
}
return(temp)
})
)
ar.cox.df1 <- do.call("rbind",ar.cox[1:10000])
ar.cox.df2 <- do.call("rbind",ar.cox[10001:20000])
ar.cox.df3 <- do.call("rbind",ar.cox[20001:30000])
ar.cox.df4 <- do.call("rbind",ar.cox[30001:40000])
ar.cox.df5 <- do.call("rbind",ar.cox[40001:50000])
ar.cox.df6 <- do.call("rbind",ar.cox[50001:60000])
ar.cox.df7 <- do.call("rbind",ar.cox[60001:70000])
ar.cox.df8 <- do.call("rbind",ar.cox[70001:80000])
ar.cox.df9 <- do.call("rbind",ar.cox[80001:length(ar.cox)])
ar.cox.df <- bind_rows(ar.cox.df9,bind_rows(ar.cox.df8,bind_rows(ar.cox.df7,bind_rows(ar.cox.df6,bind_rows(ar.cox.df5,bind_rows(ar.cox.df4,bind_rows(ar.cox.df3,bind_rows(ar.cox.df1,ar.cox.df2))))))))
rm(list=paste0("ar.cox.df",1:9))
ar.cox.df <- as_tibble(ar.cox.df)
ar.cox.df <- ar.cox.df %>% arrange(id)
# Remove observations that had branches with bills from multiple countries
ar.cox.df <- ar.cox.df[-which(ar.cox.df$id %in% ar.cox.df$id[which(ar.cox.df$country=="-88")]),]
# Make sure numeric columns are numeric
ar.cox.df[,c(1:6,9:10)] <- apply(ar.cox.df[,c(1:6,9:10)], 2, function(x){as.numeric(x)})
# Remove observations with any branches from before 1970
ar.cox.df <- ar.cox.df[-which(ar.cox.df$id %in% ar.cox.df$id[which(ar.cox.df$o.year < 1970)]),]
ar.cox.df <- ar.cox.df[-which(ar.cox.df$id %in% unique(ar.cox.df$id[which(ar.cox.df$c.year<1970)])),]
# Remove branches from after 2018
ar.cox.df <- ar.cox.df[-which(ar.cox.df$c.year>2018),]
dim(ar.cox.df)
countrycode(ar.cox.df$country,origin = "country.name",destination = "country.name")
grep(ar.cox.df$country,pattern = "; ")
grep(ar.cox.df$country,pattern = ";")
grep(ar.cox.df$country,pattern = "Niue")
grep(ar.cox.df$country,pattern = "Samoa")
dat$national[grep(dat$country,pattern = "American Samoa")]
# 21 March 2019
########################
########################
# This file contains the code for the 2019 MPSA analysis of policy durability.
# 1. Generate a long dataframe containing unique sequences of original policies and subsequent amendments
# 2. Add covariate information
# 3. Run cox models
# 4. Generate expected duration estimates and plot
########################
########################
setwd("/Volumes/GoogleDrive/My Drive/Research/policy_durability")
library(tidyverse)
library(readxl)
library(pbapply)
library(countrycode)
library(parallel)
library(simPH)
library(Amelia)
library(survival)
library(coxme)
library(cowplot)
library(coxed)
########################
#### 1. Generate a long dataframe containing unique sequences of original policies and subsequent amendments ####
### 1.1 Load the data ####
dat <- as_tibble(read_delim("/Volumes/GoogleDrive/My Drive/Research/env_lead/Code and Data/Policy Innovations/Data/faolex_dat_20Jan20.csv",delim = "t",trim_ws = TRUE))
### Clean the data
# For these purposes, need to clean:
# - Country
dat$country[which(is.na(dat$country))] = unlist(dat[which(is.na(dat$country)),"country_territory"])
dat = dat %>% select(-country_territory)
countries <- str_split(dat$country,pattern = "; ",simplify = T)
countries <- sort(unique(as.vector(unlist(countries))))
notcountries <- countries[grep(countries, pattern = "[(]")]
notcountries <- notcountries[-which(notcountries %in% c("Bolivia (Plurinational State of)", "China (Taiwan)" ))]
# - National/Multinational
dat$national <- 1
dat$national[unique(c(unlist(notcountries_idx),which(is.na(dat$territorial_subdivision)==F)))] <- 0
# - National/Multinational
dat$national <- 1
notcountries <- countries[grep(countries, pattern = "[(]")]
## set path to replication folder
curcomp <- c("Desktop/")
setwd(curcomp)
# warnings are turned off (e.g. about creating NAs in converting character strings to numerics)
# set options(warn = 0) to restore warnings
options(warn=-1)
## scripts replicating data cleaning and assembly
source("climate_rep/data/variables/ghg emissions/industry level emissions/ghg preparation R script.R")
file.exists("climate_rep/data/variables/ghg emissions/industry level emissions/ghg preparation R script.R")
getwd()
.libPaths(getwd())
paste(curcomp, "climate_rep/packages/", sep = "")
setwd(paste(curcomp, "climate_rep/packages/", sep = ""))
curcomp
curcomp <- "~Desktop/"
setwd(paste(curcomp, "climate_rep/packages/", sep = ""))
setwd("climate_rep/packages/")
paste(curcomp, "climate_rep/packages/", sep = "")
## set path to replication folder
curcomp <- c("~/Desktop/")
setwd(curcomp)
# warnings are turned off (e.g. about creating NAs in converting character strings to numerics)
# set options(warn = 0) to restore warnings
options(warn=-1)
## scripts replicating data cleaning and assembly
source("climate_rep/data/variables/ghg emissions/industry level emissions/ghg preparation R script.R")
install.packages("bindrcpp", lib="/Library/Frameworks/R.framework/Versions/3.6/Resources/library")
## set path to replication folder
curcomp <- c("~/Desktop/")
setwd(curcomp)
# warnings are turned off (e.g. about creating NAs in converting character strings to numerics)
# set options(warn = 0) to restore warnings
options(warn=-1)
## scripts replicating data cleaning and assembly
source("climate_rep/data/variables/ghg emissions/industry level emissions/ghg preparation R script.R")
install.packages("plyr", lib="/Library/Frameworks/R.framework/Versions/3.6/Resources/library")
## scripts replicating data cleaning and assembly
source("climate_rep/data/variables/ghg emissions/industry level emissions/ghg preparation R script.R")
## set path to replication folder
curcomp <- c("~/Desktop/")
setwd(curcomp)
# warnings are turned off (e.g. about creating NAs in converting character strings to numerics)
# set options(warn = 0) to restore warnings
options(warn=-1)
## scripts replicating data cleaning and assembly
source("climate_rep/data/variables/ghg emissions/industry level emissions/ghg preparation R script.R")
## set path to replication folder
curcomp <- c("~/Desktop/")
setwd(curcomp)
# warnings are turned off (e.g. about creating NAs in converting character strings to numerics)
# set options(warn = 0) to restore warnings
options(warn=-1)
## load all packages
setwd(paste(curcomp, "climate_rep/packages/", sep = "")); .libPaths(getwd())
list.files()
library(plyr, lib.loc=getwd())
suppressMessages(library(plyr, lib.loc=getwd()))
## set path to replication folder
curcomp <- c("~/Desktop/")
setwd(curcomp)
# warnings are turned off (e.g. about creating NAs in converting character strings to numerics)
# set options(warn = 0) to restore warnings
options(warn=-1)
## scripts replicating data cleaning and assembly
source("climate_rep/data/variables/ghg emissions/industry level emissions/ghg preparation R script.R")
## load all packages
setwd(paste(curcomp, "climate_rep/packages/", sep = "")); .libPaths(getwd())
library(plyr, lib.loc=getwd())
library(survival)
library(tidyverse)
library(pbapply)
load('cov_imp_29Jan20')
lm(spending ~ violentyear + other_reasons_for_spending, data = dat)
?interaction.plot
library(readxl)
dat <- read_excel("Downloads/Egypt1990-2017.xlsx")
View(dat)
table(dat$issue1)
library(tidyverse)
dat$issue1_1 <- 0
dat$issue1_1[which(dat$issue1==1)]
dat$issue1_1[which(dat$issue1==1)] <- 1
dat$issue1_1 <- 0
dat$issue1_1[which(dat$issue1==1)] <- 1
dat$issue1_2 <- 0
dat$issue1_2[which(dat$issue1==2)] <- 1
dat$issue1_3 <- 0
dat$issue1_3[which(dat$issue1==3)] <- 1
dat$issue1_4 <- 0
dat$issue1_4[which(dat$issue1==4)] <- 1
dat$issue1_5 <- 0
dat$issue1_5[which(dat$issue1==5)] <- 1
dat$issue1_6 <- 0
dat$issue1_6[which(dat$issue1==6)] <- 1
dat$issue1_7 <- 0
dat$issue1_7[which(dat$issue1==7)] <- 1
dat$issue1_8 <- 0
dat$issue1_8[which(dat$issue1==8)] <- 1
dat$issue1_9 <- 0
dat$issue1_9[which(dat$issue1==9)] <- 1
dat$issue1_10 <- 0
dat$issue1_10[which(dat$issue1==10)] <- 1
dat$issue1_11 <- 0
dat$issue1_11[which(dat$issue1==11)] <- 1
dat$issue1_12 <- 0
dat$issue1_12[which(dat$issue1==12)] <- 1
dat$repress
table(dat$repress)
library(MASS)
?polr
paste0("issue1_",1:12)
polr(repress ~ paste0("issue1_",1:12), data = dat)
polr(repress ~ issue1_1 + issue1_2 + issue1_3 + issue1_4 + issue1_5 + issue1_6 + issue1_7 + issue1_8 + issue1_9 + issue1_10 + issue1_11 + issue1_12, data = dat)
polr(factor(repress) ~ issue1_1 + issue1_2 + issue1_3 + issue1_4 + issue1_5 + issue1_6 + issue1_7 + issue1_8 + issue1_9 + issue1_10 + issue1_11 + issue1_12, data = dat)
my_model <- polr(factor(repress) ~ issue1_1 + issue1_2 + issue1_3 + issue1_4 + issue1_5 + issue1_6 + issue1_7 + issue1_8 + issue1_9 + issue1_10 + issue1_11 + issue1_12, data = dat)
summary(my_model)
summary(polr(factor(repress) + factor(issue1), data = dat))
summary(polr(factor(repress)  ~ factor(issue1), data = dat))
which(dat$issue1 %in% c(1,2,5,7)
)
factor(issue1,levels = c(12:1))
factor(dat$issue1,levels = c(12:1))
summary(polr(factor(repress) ~ factor(issue1,levels = c(12:1)), data = dat))
dat$issue1
hist(dat$npart)
table(dat$npart)
dat$npart[which(dat$npart==-99)] <- NA
hist(dat$npart)
dat$npart
hist(dat$ndeath)
table(dat$ndeath)
my_model <- polr(factor(repress) ~ factor(issue1,levels = c(1:12)) + npart, data = dat)
summary(my_model)
my_model_results <- summary(my_model)
my_model_results$coefficients
coefplot::coefplot(my_model)
dat$ndeath[which(dat$ndeath<0)]
dat$ndeath[which(dat$ndeath<0)] <- NA
dat$ndeath
hist(dat$ndeath)
hist(log(dat$ndeath + 1))
exp(dat$npart)
dat$ndeath/exp(dat$npart)
dat[913,]
View(dat[913,])
exp(5)
my_model <- polr(factor(repress) ~ factor(issue1,levels = c(1:12)) + npart + ndeath, data = dat)
my_model_results <- summary(my_model)
coefplot::coefplot(my_model)
my_model_results
my_model <- polr(factor(repress) ~ factor(issue1,levels = c(1:12)) + npart, data = dat)
my_model
summary(my_model)
install.packages("estimatr")
setwd("~/Dropbox/GuessNyhanReifler/DART0023/PNAS/post_accept/replication files/US data")
setwd("../India data/Data/")
w1 <- read_dta("accuracy-fake-tips-congenial-ftf_data.dta") # FTF w1
w1$tips <- as_factor(w1$tips)
w2 <- read_dta("accuracy-fake-tips-congenial-online_data.dta") # online w1 (ignore var name)
w2$tips <- as_factor(w2$tips)
w1 <- w1 %>% mutate(type = case_when(dv %in% 7:12 ~ "Mainstream news",
dv %in% 1:6 ~ "False news"))
w2 <- w2 %>% mutate(type = case_when(dv %in% 7:12 ~ "Mainstream news",
dv %in% 1:6 ~ "False news"))
library(tidyverse)
library(estimatr)
library(haven)
library(scales)
setwd("~/Dropbox/GuessNyhanReifler/DART0023/PNAS/post_accept/replication files/US data")
setwd("../India data/Data/")
w1 <- read_dta("accuracy-fake-tips-congenial-ftf_data.dta") # FTF w1
w1$tips <- as_factor(w1$tips)
w2 <- read_dta("accuracy-fake-tips-congenial-online_data.dta") # online w1 (ignore var name)
w2$tips <- as_factor(w2$tips)
w1 <- w1 %>% mutate(type = case_when(dv %in% 7:12 ~ "Mainstream news",
dv %in% 1:6 ~ "False news"))
w2 <- w2 %>% mutate(type = case_when(dv %in% 7:12 ~ "Mainstream news",
dv %in% 1:6 ~ "False news"))
dim(w1)
View(w1)
fig_1a <- w1 %>% split(.$type) %>%
map(~ lm_robust(binary_accuracy ~ 0 + tips, clusters = caseid, data = .x)) %>% map_df(tidy, .id = "subgroup") %>%
mutate(tips = str_replace(term, "tips", "")) %>% select(tips, subgroup, estimate, conf.low, conf.high)
fig_1b <- w2 %>% split(.$type) %>%
map(~ lm_robust(binary_accuracy ~ 0 + tips, clusters = caseid, data = .x)) %>% map_df(tidy, .id = "subgroup") %>%
mutate(tips = str_replace(term, "tips", "")) %>% select(tips, subgroup, estimate, conf.low, conf.high)
fig_1a$label <- "Face-to-face"
fig_1b$label <- "Online"
fig1 <- bind_rows(fig_1a, fig_1b)
fig1$subgroup <- as_factor(fig1$subgroup)
fig1$label <- as_factor(fig1$label)
fig1$label <- relevel(fig1$label, ref = "Online")
fig1
library(tidyverse)
library(estimatr)
library(haven)
library(scales)
setwd("~/Dropbox/GuessNyhanReifler/DART0023/PNAS/post_accept/replication files/US data")
setwd("../India data/Data/")
w1 <- read_dta("accuracy-fake-tips-congenial-ftf_data.dta") # FTF w1
w1$tips <- as_factor(w1$tips)
w2 <- read_dta("accuracy-fake-tips-congenial-online_data.dta") # online w1 (ignore var name)
w2$tips <- as_factor(w2$tips)
w1 <- w1 %>% mutate(type = case_when(dv %in% 7:12 ~ "Mainstream news",
dv %in% 1:6 ~ "False news"))
w2 <- w2 %>% mutate(type = case_when(dv %in% 7:12 ~ "Mainstream news",
dv %in% 1:6 ~ "False news"))
fig_1a <- w1 %>% split(.$type) %>%
map(~ lm_robust(binary_accuracy ~ 0 + tips, clusters = caseid, data = .x)) %>% map_df(tidy, .id = "subgroup") %>%
mutate(tips = str_replace(term, "tips", "")) %>% select(tips, subgroup, estimate, conf.low, conf.high)
fig_1b <- w2 %>% split(.$type) %>%
map(~ lm_robust(binary_accuracy ~ 0 + tips, clusters = caseid, data = .x)) %>% map_df(tidy, .id = "subgroup") %>%
mutate(tips = str_replace(term, "tips", "")) %>% select(tips, subgroup, estimate, conf.low, conf.high)
fig_1a$label <- "Face-to-face"
fig_1b$label <- "Online"
fig1 <- bind_rows(fig_1a, fig_1b)
fig1$subgroup <- as_factor(fig1$subgroup)
fig1$label <- as_factor(fig1$label)
fig1$label <- relevel(fig1$label, ref = "Online")
fig1
